function init_i18n() {
	document.title = chrome.i18n.getMessage("showimageTitle")
}
var PopupPicker = {
	init : function(c, a, b) {
		this.element = c;
		this.popup = a;
		this.attach();
		this.onPick = b.onPick;
		this.popup.removeEventListener("click");
		this.popup.addEventListener("click", function(f) {
			var d = f.target;
			if (d.tagName.toLowerCase() != "li") {
				d = d.parentNode
			}
			if (d.tagName.toLowerCase() != "li") {
				return
			}
			this.onPick(d);
			this.hide()
		}.bind(this));
		return this
	},
	attach : function() {
		PopupPicker.element.addEventListener("click", PopupPicker.show)
	},
	detach : function() {
		PopupPicker.element.removeEventListener("click", PopupPicker.show)
	},
	show : function() {
		PopupPicker.detach();
		PopupPicker.popup.style.display = "block";
		document.body.addEventListener("click", PopupPicker.bodyClicked)
	},
	hide : function() {
		PopupPicker.popup.style.display = "none";
		document.body.removeEventListener("click", PopupPicker.bodyClicked);
		setTimeout(function() {
			PopupPicker.attach()
		}, 100)
	},
	bodyClicked : function(a) {
		if (PopupPicker.popup.style.display != "none"
				&& !PopupPicker.isMouseOver(a)) {
			PopupPicker.hide()
		}
	},
	isMouseOver : function(h) {
		var a = h.pageX, i = h.pageY;
		var d = PopupPicker.popup.getBoundingClientRect();
		var g = d.left + PopupPicker.popup.clientLeft;
		var f = d.top + PopupPicker.popup.clientTop;
		var c = g + d.width;
		var b = f + d.height;
		return a > g && a < c && i > f && i < b
	}
};
var BoardPicker = {
	itemHeight : 30,
	maxVisibleItems : 8,
	setHeight : false,
	init : function(a, b) {
		this.element = a;
		this._maxH = this.itemHeight * this.maxVisibleItems;
		this.curEl = a.querySelector(".CurrentBoard"), this.popEl = a
				.querySelector(".BoardList"), this.bodyEl = a
				.querySelector(".BoardListBody");
		this.listEl = this.bodyEl.querySelector("ul");
		this.bodyEl.style.height = "0px";
		this.popup = PopupPicker.init(a, this.popEl, {
			onPick : this.select
		});
		this._items = 0;
		this.listEl.innerHTML = "";
		if (b && b.length > 0) {
			b.forEach(function(c) {
				BoardPicker._injectItem(c.board_id, c.title)
			});
			this.select(this.listEl.querySelector("li"))
		} else {
			this.curEl.innerHTML = "选择画板...";
			this.curEl.data = null
		}
		return this
	},
	_injectItem : function(d, c) {
		this._items += 1;
		var a = document.createElement("li");
		a.setAttribute("class", "BoardCategory");
		a.innerHTML = "<span>" + c + "</span>";
		a.data = d;
		this.listEl.appendChild(a);
		var b = this._items * this.itemHeight;
		this.bodyEl.style.height = (b > this._maxH ? this._maxH : b) + "px";
		return a
	},
	add : function(b) {
		var a = this._injectItem(b.board_id, b.title);
		this.select(a);
		return this
	},
	hide : function() {
		this.popup.hide()
	},
	select : function(a) {
		if (!a) {
			return
		}
		if (a.constructor === HTMLLIElement) {
			BoardPicker.curEl.innerHTML = a.querySelector("span").innerHTML;
			BoardPicker.curEl.data = a.data;
			return BoardPicker
		}
		var c = BoardPicker.listEl.querySelectorAll("li");
		for ( var d = 0, b = c.length; d < b; d++) {
			if (c[d].data == a) {
				return BoardPicker.select(c[d])
			}
		}
		return BoardPicker
	},
	getSelected : function() {
		return BoardPicker.curEl.data
	}
};
var bg = chrome.extension.getBackgroundPage();
window.addEventListener("load", function() {
	init_i18n();
	$("canvas").addEventListener("selectstart", function() {
		return false
	});
	$("mask-canvas").addEventListener("selectstart", function() {
		return false
	});
	var a = document.createElement("canvas");
	a.width = $("canvas").width = bg.screenshot.canvas.width;
	a.height = $("canvas").height = bg.screenshot.canvas.height;
	var d = a.getContext("2d");
	d.drawImage(bg.screenshot.canvas, 0, 0);
	$("canvas").getContext("2d").drawImage(a, 0, 0);
	$("canvas").style.display = "block";
	document.getElementById("btnClose").addEventListener("click", function() {
		photoshop.closeCurrentTab()
	});
	setTimeout(function() {
		UploadUI.showDialog()
	}, 100);
	var e = document
			.querySelector("#bookmarklet .ImagePicker .carousel-list li");
	var c = new Image();
	c.src = $("canvas").toDataURL("image/png");
	e.appendChild(c);
	$("description").value = bg.screenshot.page_info.text;
	$("url").value = bg.screenshot.page_info.href;
	var f = Account.getUsers(Huaban.siteId);
	var b = null;
	for (_user in f) {
		b = f[_user];
		break
	}
	UploadUI.init(b);
	UploadUI.initBoards();
	ShareButton.init();
	if (!b) {
		UploadUI.showAuth()
	} else {
		UploadUI.showUser(Huaban.siteId, b);
		UploadUI.getBoards(b)
	}
	$("auth_btn").addEventListener("click", function() {
		UploadUI.showLoading();
		UploadUI.getAccessToken(Huaban.siteId)
	});
	$("btnUpload").addEventListener("click", function() {
		photoshop.draw();
		c.src = $("canvas").toDataURL("image/png");
		UploadUI.showDialog();
		photoshop.finish()
	})
});